<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="流程ID" prop="workflowId">
        <el-input v-model="queryParams.workflowId" placeholder="请输入流程ID" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="业务ID" prop="businessId">
        <el-input v-model="queryParams.businessId" placeholder="请输入业务ID" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="业务类型" prop="businessTypeOpt">
        <el-select v-model="queryParams.businessTypeOpt" placeholder="请选择业务类型" clearable size="small">
          <el-option v-for="dict in businessTypeOptOptions" :key="dict.dictValue" :label="dict.dictLabel"
            :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker clearable size="small" style="width: 200px" v-model="queryParams.startTime" type="date"
          value-format="yyyy-MM-dd" placeholder="选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="流程状态" prop="flowStatusOpt">
        <el-select v-model="queryParams.flowStatusOpt" placeholder="请选择流程状态" clearable size="small">
          <el-option v-for="dict in flowStatusOptOptions" :key="dict.dictValue" :label="dict.dictLabel"
            :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="流程名称" prop="flowName">
        <el-input v-model="queryParams.flowName" placeholder="请输入流程名称" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="创建单位" prop="createOrgId">
        <el-input v-model="queryParams.createOrgId" placeholder="请输入创建单位" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker clearable size="small" style="width: 200px" v-model="queryParams.endTime" type="date"
          value-format="yyyy-MM-dd" placeholder="选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['labdata:flowInstance:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['labdata:flowInstance:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['labdata:flowInstance:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5" v-show="false">
        <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['labdata:flowInstance:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="flowInstanceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="实例ID" align="center" prop="instanceId" />
      <el-table-column label="流程ID" align="center" prop="workflowId" />
      <el-table-column label="业务类型" align="center" prop="businessTypeOpt" :formatter="businessTypeOptFormat" />
      <el-table-column label="开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="流程状态" align="center" prop="flowStatusOpt" :formatter="flowStatusOptFormat" />
      <el-table-column label="流程名称" align="center" prop="flowName" />
      <el-table-column label="创建人员" align="center" prop="createBy" />
      <el-table-column label="创建单位" align="center" prop="createOrgId" />
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['labdata:flowInstance:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['labdata:flowInstance:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改工作流实例对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="流程ID" prop="workflowId">
          <el-input v-model="form.workflowId" placeholder="请输入流程ID" />
        </el-form-item>
        <el-form-item label="业务ID" prop="businessId">
          <el-input v-model="form.businessId" placeholder="请输入业务ID" />
        </el-form-item>
        <el-form-item label="业务类型" prop="businessTypeOpt">
          <el-select v-model="form.businessTypeOpt" placeholder="请选择业务类型">
            <el-option v-for="dict in businessTypeOptOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker clearable size="small" style="width: 200px" v-model="form.startTime" type="date"
            value-format="yyyy-MM-dd" placeholder="选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="处理角色" prop="flowRoles">
          <el-input v-model="form.flowRoles" placeholder="请输入处理角色" />
        </el-form-item>
        <el-form-item label="流程状态" prop="flowStatusOpt">
          <el-select v-model="form.flowStatusOpt" placeholder="请选择流程状态">
            <el-option v-for="dict in flowStatusOptOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="流程名称" prop="flowName">
          <el-input v-model="form.flowName" placeholder="请输入流程名称" />
        </el-form-item>
        <el-form-item label="创建单位" prop="createOrgId">
          <el-input v-model="form.createOrgId" placeholder="请输入创建单位" />
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker clearable size="small" style="width: 200px" v-model="form.endTime" type="date"
            value-format="yyyy-MM-dd" placeholder="选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="抄送人员" prop="copyToUsers">
          <el-input v-model="form.copyToUsers" placeholder="请输入抄送人员" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listFlowInstance,
    getFlowInstance,
    delFlowInstance,
    addFlowInstance,
    updateFlowInstance,
    exportFlowInstance
  } from "@/api/labdata/flowInstance";

  export default {
    name: "FlowInstance",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 工作流实例表格数据
        flowInstanceList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 业务类型字典
        businessTypeOptOptions: [],
        // 流程状态字典
        flowStatusOptOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          workflowId: null,
          businessId: null,
          businessTypeOpt: null,
          startTime: null,
          flowStatusOpt: null,
          flowName: null,
          createOrgId: null,
          endTime: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          businessId: [{
            required: true,
            message: "业务ID不能为空",
            trigger: "blur"
          }],
          businessTypeOpt: [{
            required: true,
            message: "业务类型不能为空",
            trigger: "change"
          }],
          flowStatusOpt: [{
            required: true,
            message: "流程状态不能为空",
            trigger: "change"
          }],
          flowName: [{
            required: true,
            message: "流程名称不能为空",
            trigger: "blur"
          }],
          createBy: [{
            required: true,
            message: "创建人员不能为空",
            trigger: "blur"
          }],
          createOrgId: [{
            required: true,
            message: "创建单位不能为空",
            trigger: "blur"
          }],
        }
      };
    },
    created() {
      this.getList();
      this.getDicts("businessTypeOpt").then(response => {
        this.businessTypeOptOptions = response.data;
      });
      this.getDicts("flowStatusOpt ").then(response => {
        this.flowStatusOptOptions = response.data;
      });
    },
    methods: {
      /** 查询工作流实例列表 */
      getList() {
        this.loading = true;
        listFlowInstance(this.queryParams).then(response => {
          this.flowInstanceList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 业务类型字典翻译
      businessTypeOptFormat(row, column) {
        return this.selectDictLabel(this.businessTypeOptOptions, row.businessTypeOpt);
      },
      // 流程状态字典翻译
      flowStatusOptFormat(row, column) {
        return this.selectDictLabel(this.flowStatusOptOptions, row.flowStatusOpt);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          instanceId: null,
          workflowId: null,
          businessId: null,
          businessTypeOpt: null,
          startTime: null,
          flowRoles: null,
          flowStatusOpt: null,
          flowName: null,
          createBy: null,
          createOrgId: null,
          endTime: null,
          copyToUsers: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.instanceId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加工作流实例";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const instanceId = row.instanceId || this.ids
        getFlowInstance(instanceId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改工作流实例";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.instanceId != null) {
              updateFlowInstance(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addFlowInstance(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const instanceIds = row.instanceId || this.ids;
        this.$confirm('是否确认删除工作流实例编号为"' + instanceIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delFlowInstance(instanceIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有工作流实例数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportFlowInstance(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
      }
    }
  };
</script>
